import { _decorator, Component, Node, Label, SpriteFrame, Sprite } from 'cc';
import { BundlesConst } from '../../ccc3-framework-core/const/Const';
import ItemRender from '../../ccc3-framework-core/framework/extends/ListView/ItemRender';
import { BaseObject } from '../../ccc3-framework-core/framework/modules/baseObject';
import dataUtil from '../../ccc3-framework-core/framework/modules/dataUtil';
import { entityUtil } from '../../ccc3-framework-core/framework/modules/entityUtil';
import eventUtil from '../../ccc3-framework-core/framework/modules/eventUtil';
import { memoryConfig } from '../../ccc3-framework-core/framework/modules/memoryConfig';
import { resourceUtil } from '../../ccc3-framework-core/framework/modules/resourceUtil';
import { uiManager } from '../../ccc3-framework-core/framework/modules/uiManager';
import { gameState } from '../../ccc3-framework-core/gameState';
import Common from '../../ccc3-framework-core/utils/Common';
import { BundlesDefine, CategoryDefine, eventDefine, formDefine, pathDefine } from '../CustomDefine';
const { ccclass, property } = _decorator;

@ccclass('mapItemRender')
export class mapItemRender extends ItemRender {

    @property(Sprite)
    public imgLogo: Sprite = null;

    @property(Label)
    public txtLevel: Label = null;

    @property(Node)
    public imgLock: Node = null;

    @property(Node)
    public imgUnLock: Node = null;

    @property(Node)
    public imgCurrent: Node = null;

    start() {
        this.bindTouchEnd(this.node, this.onStartMap);
    }

    public dataChanged() {
        this.data.index = this.data.ID - 1;
        let data = this.data;
        this.txtLevel.string = `关卡 ${data.ID}`;
        let url = `map/icon_lv${Common.padLeft("" + this.data.ID, 2, "0")}/spriteFrame`;
        resourceUtil.loadBundleRes<SpriteFrame>(BundlesDefine.images, url, (err, sf) => {
            if (err) {
                return;
            }
            this.imgLogo.spriteFrame = sf;
        })
        this.reset(data);
    }

    public reset(data) {
        this.imgCurrent.active = this.data.index == dataUtil.getCurrentLevel();
        if (this.imgCurrent.active) {
            this.imgLock.active = false;
            this.imgUnLock.active = false;
        }
        else {
            this.imgLock.active = this.data.index > dataUtil.getCurrentLevel();
            this.imgUnLock.active = this.data.index < dataUtil.getCurrentLevel();
        }
    }


    private onStartMap() {
        if (this.data.index > dataUtil.getCurrentLevel()) {
            uiManager.showToast("请依次解锁关卡");
            return;
        }

        let local_fatigue_num = dataUtil.getUnlockNumByDetailId(1, CategoryDefine.FATIGUE);
        if (local_fatigue_num < 0) {
            uiManager.pushUIForm(pathDefine.formDir + formDefine.fatigueForm, null, BundlesDefine.main);
            return;
        }


        uiManager.hideUIForm(formDefine.mapForm);
        uiManager.hideUIForm(formDefine.homeForm);
        eventUtil.dispatchEvent(eventDefine.SHOW_LOADING, true);

        memoryConfig.instance.level = parseInt(this.data.index);
        gameState.instance.startGame(memoryConfig.instance.level);
    }

    update(deltaTime: number) {

    }
}

